package com.mofang.powerdekorhelper.utils;

import android.os.Environment;
import android.os.StatFs;

import java.io.File;

//SD卡相关的辅助类
public class SDCardUtils {

    /**
     * 创建主目录(/longran)
     *
     * @return
     */
    public static String getResourcePath() {
        String path = getSDCardPath() + "powerdekor" + File.separator;
        if (fileIsExists(getSDCardPath())) {
            if (!fileIsExists(path)) {
                File file = new File(path);
                file.mkdirs();
            }
        }
        return path;
    }

    /**
     * 根据文件名创建目录(/longran/path/)
     *
     * @return datePath
     */

    public static String getPath(String path) {
        String filePath = getResourcePath();
        String datePath = null;
        if (fileIsExists(filePath)) {
            datePath = filePath + path + File.separator;
            File file = new File(datePath);
            file.mkdirs();
        }
        return datePath;
    }

    /**
     * 场景路径
     */
    public static String marScenePath = getResourcePath() + "data/scene" + File.separator;

    public static String getScenePath(String sceneType) {
        String sceneDir = marScenePath + sceneType + File.separator;
        return sceneDir;
    }

    /**
     * 获取产品库或数据库文件路径
     *
     * @param ProductType 产品类型
     * @param brandCode   品牌代号
     * @return
     */
    public static String getProductPath(String ProductType, String brandCode) {

        String path = "data" + File.separator + ProductType + File.separator + brandCode;

        return path;
    }

    /**
     * 硬盘缓存地址
     */
    public static String marcpoloCachePath = getResourcePath() + "imageloader/Cache";

    /**
     * 判断SDCard是否可用
     */
    public static boolean isSDCardEnable() {
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }

    /**
     * 获取SD卡根路径
     */
    public static String getSDCardPath() {
        return Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator;
    }

    /**
     * 获取系统存储路径
     */
    public static String getRootDirectoryPath() {
        return Environment.getRootDirectory().getAbsolutePath();
    }

    /**
     * SDCARD exists
     */
    public static boolean externalMemoryAvailable() {
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }

    public static boolean fileIsExists(String sdPath) {
        return new File(sdPath).exists();
    }

    /**
     * 获取SD卡的剩余容量 单位byte
     */
    public static long getSDCardAllSize() {
        if (isSDCardEnable()) {
            StatFs stat = new StatFs(getSDCardPath());
            // 获取空闲的数据块的数量
            long availableBlocks = (long) stat.getAvailableBlocks() - 4;
            // 获取单个数据块的大小（byte）
            long freeBlocks = stat.getAvailableBlocks();
            return freeBlocks * availableBlocks;
        }
        return 0;
    }

    /**
     * 获取指定路径所在空间的剩余可用容量字节数，单位byte
     *
     * @param filePath
     * @return 容量字节 SDCard可用空间，内部存储可用空间
     */
    public static long getFreeBytes(String filePath) {
        // 如果是sd卡的下的路径，则获取sd卡可用容量
        if (filePath.startsWith(getSDCardPath())) {
            filePath = getSDCardPath();
        } else {// 如果是内部存储的路径，则获取内存存储的可用容量
            filePath = Environment.getDataDirectory().getAbsolutePath();
        }
        StatFs stat = new StatFs(filePath);
        long availableBlocks = (long) stat.getAvailableBlocks() - 4;
        return stat.getBlockSize() * availableBlocks;
    }

}
